<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/header.jsp" %>
<script type="text/javascript">


//页面初始化
	var data = $("#openFlag").val();
	initButton(data);
	$("#deptInfoDetailForm").validate({
	    errorElement: 'div',
	    errorClass: 'help-block',
	    focusInvalid: false,
	    rules: {
	    	"deptInfo.deptName": {
	          required: true
	       },
	       "deptInfo.deptStatus": {
		          required: true
		    },
		   "deptInfo.deptId": {
			          required: true,
			          minlength:'8',
		    }
	    },
	    messages: {
	    	"deptInfo.deptName": {
	           required: "请输入部门名称!"
	        },
	        "deptInfo.deptStatus": {
		           required: "请选择部门状态!"
		    },
			"deptInfo.deptId": {
				   required: "请输入部门编号!"
			}
	    },
	    highlight: function (e) {
	        $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
	    },
	    success: function (e) {
	        $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
	        $(e).remove();
	    },
	    errorPlacement: function (error, element) {
	        error.insertAfter(element.parent());
	    }
});

//初始化按钮显示
function initButton(data){
	if(data == "1"){//新增
		$("#btnSave").show();
		$("#btnCancle").show();
		$("#btnModify").hide();
		$("#btnDelete").hide();
		$("#dept_creater").hide();
		$("#dept_createTime").hide();
		$("#dept_updater").hide();
		$("#dept_updateTime").hide();
		//$("#deptId").val("系统会自动生成部门编号，无需人工输入");
	}else if(data == "3"){//查询
		$("#btnSave").hide();
		$("#btnCancle").hide();
		$("#btnModify").show();
		$("#btnDelete").show();
		disabledAll();
	}else{//2 modifyDept
		$("#btnSave").show();
		$("#btnCancle").show();
		$("#btnModify").hide();
		$("#btnDelete").hide();
		enabledAll();
		
	}
}
function disabledAll(){
	$("#deptInfoDetailForm input[type='text']").attr("disabled",true);
	$("#deptInfoDetailForm select").attr("disabled",true);
}
function enabledAll(){
	$("#deptInfoDetailForm input[type='text']").attr("disabled",false);
	$("#deptStatus").attr("disabled",false);
}
//点击修改按钮对其他按钮的显示设置
$("#btnModify").click(function(){
	$("#btnSave").show();
	$("#btnCancle").show();
	$("#btnModify").hide();
	$("#btnDelete").hide();
	$("#openFlag").val("2");
	$("#deptId").attr("readonly",true);
	enabledAll();
});

//取消修改
$("#btnCancle").click(function(){
	var data =$("#openFlag").val();
	if(data == "1"){
		$("#deptDiv").hide();
		return;
	}
	var iscancle=window.confirm("确定取消修改？");
	if(iscancle==true){
		document.deptInfoDetailForm.reset();
		$("#btnSave").hide();
		$("#btnCancle").hide();
		$("#btnModify").show();
		$("#btnDelete").show()
		disabledAll();
	}
	else{
		return;
	}
});
//保存操作
$("#btnSave").click(function(){
	if ($("#deptInfoDetailForm").valid() == false) {
		return;
}	
		var openFlag = $("#openFlag").val();
		var urlVal ="${ctx}/org/saveDept";
		if(openFlag == "2"){
			urlVal="${ctx}/org/updateDept";
		}
		saveDeptInfo(urlVal);
});
function saveDeptInfo(urlVal){
	var params ={
			"deptId":$("#deptId").val(),
			"deptName":$("#deptName").val(),
			"deptLevel":$("#deptLevel").val(),
			"deptStatus":$("#deptStatus").val(),
			"belongOrg":$("#belongOrg").val(),
			"parentDept":$("#parentDept").val(),
			"createTime":$("#deptCreateTime").val(),
			"updateUser":$("#deptUpdater").val(),
			"updateTime":$("#deptUpdateTime").val(),
			"presidentId":$("#presidentId").val(),	
			"toOrgId":$("#toOrgId").val(),
			"oriDeptId":$("#oriDeptId").val()
	};
	$.ajax({
		async : false,
		url:urlVal,
		type:"post",
		data:$.param(params),
		dataType:"json",
		success:function(msg){
			if(msg.success != null){
				alert(msg.success);
				initButton("3");
				//var treeNode = zTree.getSelectedNodes()[0];
				freshTreeNode(zTree.getSelectedNodes()[0]);
				//alert(msg.retDeptId);
				freshDeptPage(msg.retDeptId);
			}else{
				alert(msg.fail);
			}
		}
	});
	 
}

//删除部门操作（部门下面没子节点并且部门的状态）
$("#btnDelete").click(function(){
	if($("#deptStatus").val()!='0'){
		alert("只能删除未启用的部门！");
		return;
	}
	var params ={
			"deptId":$("#deptId").val(),
			"deptName":$("#deptName").val(),
			"deptLevel":$("#deptLevel").val(),
			"deptStatus":$("#deptStatus").val(),
			"belongOrg":$("#belongOrg").val(),
			"parentDept":$("#parentDept").val(),
			"createUser":$("#createUser").val(),
			"createTime":$("#createTime").val(),
			"updateUser":$("#updateUser").val(),
			"updateTime":$("#updateTime").val(),
			"presidentId":$("#presidentId").val(),	
			"toOrgId":$("#toOrgId").val()
	};
	if(confirm("是否确定删除!")){
		$.ajax({
		async : false,
		url : "${ctx}/org/deleteDept",
		type : "post",
		data : $.param(params),
		dataType : "json",
		success : function(data){
			if(data.success != null){
				alert(data.success);
				$("#deptDiv").hide();
				var param = {"checkType":"dept","isCheckOrg":"1"};
				$("#myTreeDiv").load("${ctx}/org/haveRootNodes",param);
				}else{
					alert(data.fail);
				}
			} ,
		error: function(ret){
            alert(ret.responseText);            
            disabledAll();
      }
	}); 
}
});
//刷新节点
function freshTreeNode(treeNode){
	if(treeNode != null){
	var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
	if(treeNode.children != null ){
		zTree.reAsyncChildNodes(treeNode, "refresh");
		//alert("刷新treeNode");
	}else{
		zTree.reAsyncChildNodes(treeNodeP, "refresh");
		//alert("刷新treeNodeP");
	}
	}else{
		var param = {"checkType":"dept","isCheckOrg":"1"};
		$("#myTreeDiv").load("${ctx}/org/haveRootNodes",param);
	}
}
//刷新部门页面
function freshDeptPage(deptId){
	var param = {"deptId":deptId};
	$("#deptDetailForm").show();
	$("#deptDetailForm").load("${ctx}/org/deptDetail",param);
	
}
</script>
<div class="row" id="deptDiv">
	<div class="col-xs-12">	
		<div class="widget-box widget-color-blue2">			
			<div class="widget-header">
				<h5 class="widget-title">部门信息</h5>
				<!-- #section:custom/widget-box.toolbar -->
				<div class="widget-toolbar">
					<a href="#" data-action="fullscreen" class="orange2"><i class="ace-icon fa fa-expand"></i></a>
					<a href="#" data-action="collapse"><i class="ace-icon fa fa-chevron-up"></i></a>
					<a href="#" data-action="close"><i class="ace-icon fa fa-times"></i></a>
				</div>
				<!-- /section:custom/widget-box.toolbar -->
			</div>
			<div class="widget-body">
				<div class="widget-main no-padding">				
					<form class="form-horizontal" id="deptInfoDetailForm" name="deptInfoDetailForm">
					<input type="hidden" id="openFlag" name="openFlag" value="${openFlag}"/>
					 <input type="hidden" id="belongOrg"  name ="deptInfo.belongOrg" value="${deptInfo.belongOrg}">
					 <input type="hidden" id="parentDept"  name ="deptInfo.parentDept" value="${deptInfo.parentDept}">
	                <input type="hidden" id="deptLevel"  name="deptInfo.deptLevel" value="${deptInfo.deptLevel}">
	                 <input type="hidden" id="toOrgId"  name="toOrgId" value="${deptInfo.toOrgId}">	
	                 <input type="hidden" id="oriDeptId"  name="oriDeptId" value="${deptInfo.deptId}">
					 	<fieldset>
						    <div class="row">
						    	<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="deptId">部门编号<font color="red">*</font></label>
									<div class="col-sm-8">
									<div class="clearfix">
										<input type="text"  id="deptId" maxlength="15"
					                           name="deptInfo.deptId" value="${deptInfo.deptId}" class="col-xs-12" />
									</div>
									</div>
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="deptLevelName">部门层级</label>
									<div class="col-sm-8">
										<select class="chosen-select col-xs-12" id="deptLevelName" name="deptLevelName" disabled>
										<hw:dictOption dictType="D00031" value="${deptInfo.deptLevel}"/>
										</select>
									</div>
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="deptName">部门名称<font color="red">*</font></label>
									<div class="col-sm-8">
									<div class="clearfix">
										<input type="text" title='部门名称不能超过30个字!'  maxlength="30" id="deptName" name="deptInfo.deptName"
					                           value="${deptInfo.deptName}"class="col-xs-12"/>
									</div>
									</div>
								</div>							
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="deptStatus">部门状态<font color="red">*</font></label>
									<div class="col-sm-8">
									<div class="clearfix">
										<select class="chosen-select col-xs-12" id="deptStatus" name="deptInfo.deptStatus">
										<hw:dictOption dictType="D00027" value="${deptInfo.deptStatus}"/>
										</select>
										</div>
									</div>
								</div>							
						    	<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="belongOrgName">所属机构</label>
									<div class="col-sm-8">
										<input type="text" id="belongOrgName" name="belongOrgName" value='<hw:orgName orgId="${deptInfo.belongOrg}"/>' class="col-xs-12" readonly />
									</div>
								</div>
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="parentDeptName">上级部门</label>
									<div class="col-sm-8">
									<input type="text"  id="parentDeptName"  name="parentDeptName" class="col-xs-12" value='<hw:deptName deptId="${deptInfo.parentDept }" />' readonly />
									</div>
								</div>	
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="presidentId">分管行长</label>
									<div class="col-sm-8">
										<input type="text" id="presidentId" name="deptInfo.presidentId"  maxlength="30" value="${deptInfo.presidentId}" class="col-xs-12" />
									</div>
								</div>	
								<div class="form-group col-xs-6">
									<label class="col-sm-4 control-label no-padding-left" for="toOrgIdName">统计归属机构</label>
									<div class="col-sm-8">
										<input type="text" id="toOrgIdName" name="toOrgIdName" value="<hw:orgName orgId='${deptInfo.toOrgId }'/>" class="col-xs-12" readonly />
									</div>
								</div>		
								<div class="form-group col-xs-6" id ="dept_creater">
									<label class="col-sm-4 control-label no-padding-left" for="deptCreater">创建人</label>
									<div class="col-sm-8">
										<input type="text" id="deptCreater" name="deptInfo.createUser" value="<hw:userName userId='${deptInfo.createUser}' />" class="col-xs-12" readonly/>
									</div>
								</div>
								<div class="form-group col-xs-6" id="dept_createTime">
									<label class="col-sm-4 control-label no-padding-left" for="deptCreateTime">创建时间</label>
									<div class="col-sm-8">
										<input type="text" id="deptCreateTime" name="deptInfo.createTime" value="${deptInfo.createTime }" class="col-xs-12" readonly/>
									</div>
								</div>	
								
								<div class="form-group col-xs-6" id="dept_updater">
									<label class="col-sm-4 control-label no-padding-left" for="deptUpdater">修改人</label>
									<div class="col-sm-8">
										<input type="text" id="deptUpdater" name="deptInfo.updateUser" value="<hw:userName userId='${deptInfo.updateUser}' />" class="col-xs-12" readonly />
									</div>
								</div>		
								<div class="form-group col-xs-6" id="dept_updateTime">
									<label class="col-sm-4 control-label no-padding-left" for="deptUpdateTime">修改时间</label>
									<div class="col-sm-8">
										<input type="text" id="deptUpdateTime" name="deptInfo.updateTime" value="${deptInfo.updateTime }" class="col-xs-12" readonly />
									</div>
								</div>
							</div>
						</fieldset>
						<div class="form-actions center">
						<div class="btn-group">
							<button class="btn btn-success btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"  type="button" id="btnSave">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-floppy-o bigger-110"></i>
								保存
								</span>
							</button>
							</div>
							&nbsp; &nbsp; &nbsp;
							<div class="btn-group">
							<button class="btn btn-primary btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" id="btnModify">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-pencil-square-o bigger-110"></i>
								修改
								</span>
							</button>
							</div>
							&nbsp; &nbsp; &nbsp;
							<div class="btn-group">
							<button class="btn btn-danger btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" id="btnDelete">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-trash-o bigger-110"></i>
								删除
								</span>
							</button>
							</div>
							&nbsp; &nbsp; &nbsp;
							<div class="btn-group">
							<button class="btn btn-warning btn-minier ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" id="btnCancle">
								<span class="ui-button-text">
								<i class="ace-icon fa fa-undo bigger-110"></i>
								取消
								</span>
							</button>
							</div>
						</div>		
					</form>
				</div>
			</div>
		</div>
	</div>
</div>